package com.hqyj.mall.xiaomimall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.mall.xiaomimall.entity.Items;
import com.hqyj.mall.xiaomimall.json.R;
import com.hqyj.mall.xiaomimall.service.ItemsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {

    @Resource
    private ItemsService itemsService;


    @GetMapping("/query/{id}")
    public R queryItemById(@PathVariable("id") Integer id){
        try{
            return R.success("查询商品详情成功！",itemsService.queryItemById(id));
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/catalog/{catalogName}")
    public R queryCatalogListByCatalogName(@PathVariable("catalogName") String catalogName) {
        try {
            List<Items> itemsList = itemsService.queryItemsListByCatalogName(catalogName);
            return R.success("查询指定板块下的商品列表成功！", itemsList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/pager/catalog")
    public R queryCatalogListByCatalogName(@RequestParam("catalogName") String catalogName,@RequestParam("currentPageNumber") Integer currentPageNumber) {
        try {
            Page<Items> page = itemsService.queryItemsPagerByCatalogName(catalogName,currentPageNumber);
            return R.success("查询指定板块下的商品分页列表成功！", page);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
